package com.xspace.android.xafcommon.view.tree.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.xspace.android.xafcommon.view.tree.bean.Node;
import com.xspace.android.xafcommon.view.tree.bean.TreeHelper;

import java.util.ArrayList;
import java.util.List;


/**
 * http://blog.csdn.net/lmj623565791/article/details/40212367
 * @author zhy
 *
 * @param <T>
 */
public abstract class TreeListViewAdapter<T> extends BaseAdapter {

	protected Context mContext;
	/**
	 * 存储所有可见的Node
	 */
	protected List<Node> mNodes;
	protected LayoutInflater mInflater;
	/**
	 * 存储所有的Node
	 */
	protected List<Node> mAllNodes;
	protected boolean hasHeader;
	/**
	 * 点击的回调接口
	 */
	private OnTreeNodeClickListener onTreeNodeClickListener;

	public interface OnTreeNodeClickListener {
		void onClick(Node node, int position, boolean isImg);
	}

	public void setOnTreeNodeClickListener(OnTreeNodeClickListener onTreeNodeClickListener) {
		this.onTreeNodeClickListener = onTreeNodeClickListener;
	}

	/**
	 * 
	 * 方法描述 :重新初始化数据
	 * @param datas
	 * @param defaultExpandLevel
	 */
	public void setData(List<T> datas, int defaultExpandLevel) {
		try {
			mAllNodes = TreeHelper.getSortedNodes(datas, defaultExpandLevel);
			/**
			 * 过滤出可见的Node
			 */
			mNodes = TreeHelper.filterVisibleNode(mAllNodes);

		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param mTree
	 * @param context
	 * @param datas
	 * @param defaultExpandLevel
	 *            默认展开几级树
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public TreeListViewAdapter(ListView mTree, Context context, List<T> datas, int defaultExpandLevel)
			throws IllegalArgumentException, IllegalAccessException {
		mContext = context;
		/**
		 * 对所有的Node进行排序
		 */
		mAllNodes = TreeHelper.getSortedNodes(datas, defaultExpandLevel);
		/**
		 * 过滤出可见的Node
		 */
		mNodes = TreeHelper.filterVisibleNode(mAllNodes);
		mInflater = LayoutInflater.from(context);
		/**
		 * 设置节点点击时，可以展开以及关闭；并且将ItemClick事件继续往外公布
		 */
		dataControl(mTree);

	}

	/**
	 * 相应ListView的点击事件 展开或关闭某节点
	 * 
	 * @param position
	 */
	public void dataControl(ListView mTree) {
		mTree.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (onTreeNodeClickListener != null) {
					onTreeNodeClickListener.onClick(mNodes.get(position), position, false);
				}
				/*				if(hasHeader){
									if (onTreeNodeClickListener != null)
									{
										Node node=null;
										if(position>0){
											expandOrCollapse(position-1);
											node=mNodes.get(position-1);
											onTreeNodeClickListener.onClick(node,
													position,false);
										}
									}
								}
								else{
									expandOrCollapse(position);
								if (onTreeNodeClickListener != null)
								{
									onTreeNodeClickListener.onClick(mNodes.get(position),
											position,false);
								}
								}*/
			}

		});
	}

	public void oit(int position) {
		expandOrCollapse(position);
	}

	public void oit(int position, boolean isImg) {

		if (hasHeader) {
			if (onTreeNodeClickListener != null) {
				Node node = null;
				if (position > 0) {
					expandOrCollapse(position - 1);
					node = mNodes.get(position - 1);
					onTreeNodeClickListener.onClick(node, position, isImg);
				}
			}
		} else {
			expandOrCollapse(position);
			if (onTreeNodeClickListener != null) {
				onTreeNodeClickListener.onClick(mNodes.get(position), position, isImg);
			}
		}
	}

	
	public void expandOrCollapse(int position) {
		Node n = mNodes.get(position);

		if (n != null)// 排除传入参数错误异常
		{
			if (!n.isLeaf()) {
				n.setExpand(!n.isExpand());
				mNodes = TreeHelper.filterVisibleNode(mAllNodes);
				notifyDataSetChanged();// 刷新视图
			}
		}
	}
//	/**
//	 * 
//	 * 方法描述 : 该方法实现的功能描述
//	 * @param position
//	 */
//	public void collapseAllButPost(Node node) {
//		for (Node n : mNodes) {
//			if (n != null) {
//				if (!n.isLeaf()) {
//					if (node.getId() != null && node.getId().equals(n.getId())) {
//						n.setExpand(true);
//					} else {
//						n.setExpand(false);
//					}
//				}
//			}
//		}
//		mNodes = TreeHelper.filterVisibleNode(mAllNodes);
//		notifyDataSetChanged();// 刷新视图
//
//	}

	private void collapseAllParent() {
		for (Node n : mNodes) {
			if (n != null) {
				if (!n.isLeaf()) {
					n.setExpand(false);
				}
			}
		}
	}

	/**
	 * 
	 * 方法描述 : 得到目前的所有打开并且有子元素的的节点列表
	 * @return
	 */
	public List<String> getLastNodeId() {
		List<String> resultList = new ArrayList<String>();
		for (Node n : mNodes) {
			if (n.isExpand() && n.getChildren().size() > 0) {
				resultList.add(n.getId());
			}
		}
		return resultList;
	}

	/**
	 * 
	 * 方法描述 : 打开指定id列表
	 * @param idList
	 */
	public void collapseNodeIdList(List<String> idList) {
		collapseAllParent();
		for (Node n : mNodes) {
			if (n != null) {
				if (!n.isLeaf()) {
					for (String id : idList) {
						if (id.equals(n.getId())) {
							n.setExpand(true);
							break;
						}
					}
				}
			}
		}
		mNodes = TreeHelper.filterVisibleNode(mAllNodes);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mNodes.size();
	}

	@Override
	public Object getItem(int position) {
		return mNodes.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Node node = mNodes.get(position);
		convertView = getConvertView(node, position, convertView, parent);
		// 设置内边距
		int padleft = getPadLeft(node.getLevel());
		ViewGroup vg = (ViewGroup) convertView;
		View oneView = vg.getChildAt(0);
		if (oneView != null) {
			//			oneView.setPadding(padleft, 0, 10, 0);
			oneView.setPadding(padleft+10, 0, 20, 0);
		} else {
			convertView.setPadding(padleft, 3, 3, 3);
		}
		return convertView;
	}

	public abstract View getConvertView(Node node, int position, View convertView, ViewGroup parent);

	private int getPadLeft(int level) {
		if (level == 1) {
			return 60;
		} else if (level > 1) {
			return 60 + (level - 1) * 30;
		}
		return 0;
	}
}
